<?php
/**
 * Model
 * @filesource
 */
/**
 * Namespace
 */
namespace framework\library;
/**
 * Dependencies
 */
use framework\library\database\DB;
use framework\library\interfaces\DB_Interface;
/**
 * Model Class
 *
 * Plurious Core
 *
 * Base Model implementing Simple PDO Abstraction classes
 * 
 * @author Randy Casburn <randy@plurious.com>
 * @copyright Copyright Plurious Software, all rights reserved
 * @license MIT
 * @license http://www.opensource.org/licenses/mit-license.html
 * @link http://plurious.com/framework/documentation/classes/Front.html
 * @version 2.1
 * 
 * @uses framework\library\database\DB
 */
class Model implements DB_Interface {

    /** Holds instance of the PDO Abstraction class chain */
    protected $db;

    /** Holds Column name for sorting operations */
    private $order_by_col;

    /** Holds sorting direction (ASC, DESC) for sorting operations */
    private $order_direction;

    /**
     * Creates records an instance of the PDO Abstraction class chain.
     * 
     * @param array $db_params (Temporary unitl configuration is setup)
     */
    public function __construct($name= DB_DATABASE, $user= DB_USERNAME, $pw= DB_PASSWORD, $type = DB_DRIVER, $host= DB_HOSTNAME, $port="") {
        $this->db = DB::getInstance($type, $host, $name, $user, $pw, $port);
    }

    /**
     * Sends an INSERT query to the PDO object.
     * 
     * This method is synonymous with set() & save() methods
     * 
     * @param array $data Data to insert
     * @param string $table Table to insert into
     * @return int ID of inserted row
     */
    public function insert($table, Array $data) {
        return $this->db->insert($table, $data);
    }

    /**
     * Sends an INSERT query to the PDO object.
     * 
     * This method is synonymous with insert() method
     * 
     * @param array $data Data to insert
     * @param string $table Table to insert into
     * @return int ID of inserted row
     */
    public function set($table, Array $data) {
        return $this->db->insert($table, $data);
    }

    /**
     * Sends an INSERT query to the PDO object.
     * 
     * This method is synonymous with insert() method
     * 
     * @param array $data Data to insert
     * @param string $table Table to insert into
     * @return int ID of inserted row
     */
    public function save($table, Array $data) {
        return $this->db->insert($table, $data);
    }

    /**
     * Generic PDO Query method.
     * 
     * User must supply complete SQL statement.
     * 
     * @param string $sql The SQL statement to execute
     * @param array $data The data for the query
     * @return mixed Array if SELECT, otherwise affected rows
     */
    public function query($sql, Array $data) {
        return $this->db->query($sql, $data);
    }

    /**
     * Sends a SELECT query to the PDO object.
     * 
     * This query is synonymous with the select() method.
     * 
     * @param string $table The Table to query
     * @param array $where The WHERE clause equivilance
     * @param int $limit The return limit
     * @return array Associative array of DB results
     */
    public function select($table, Array $where= array(), $limit=1000) {
        return $this->db->select($table, $where, $limit);
    }

    /**
     * Sends a SELECT query to the PDO object.
     * 
     * This query is synonymous with the select() method.
     * 
     * @param string $table The Table to query
     * @param array $where The WHERE clause equivilance
     * @param int $limit The return limit
     * @return array Associative array of DB results
     */
    public function selectCols($table, Array $cols=array(), Array $where= array(), $limit=1000) {
        return $this->db->selectCols($table, $cols, $where, $limit);
    }

    /**
     * Sends a SELECT query to the PDO object.
     * 
     * This query is synonymous with the select() method.
     * 
     * @param string $table The Table to query
     * @param array $where The WHERE clause equivilance
     * @param int $limit The return limit
     * @return array Associative array of DB results
     */
    public function get($table, Array $where= array(), $limit=1000) {
        return $this->select($table, $where, $limit);
    }

    /**
     * Sends a DELETE query to the PDO object.
     * 
     * @param type $table The Table name to delete rows from
     * @param array $where The WHERE clause (cannot be empty)
     * @return int Number of deleted rows
     */
    public function delete($table, Array $where) {
        return $this->db->delete($table, $where);
    }

    /**
     *
     * @param array $new_values The data used for the update 
     * @param array $where The WHERE clause 
     * @param string $table Name of the table to query
     * @return int Number of affected rows
     */
    public function update(Array $new_values, $table, Array $where) {
        return $this->db->update($new_values, $table, $where);
    }

    /**
     * Sets the Column and Direction for sort operations.
     * 
     * NOTE: you must reset these parameters when finished with them. Otherwise
     * all results following will be sorted using these criteria.
     * 
     * To clear the sort criteria simply call this method with 
     * no parameters passed -- setOrder();
     * 
     * @param string $col The name of the column to sort results on
     * @param string $dir Should be ASC or DESC 
     */
    public function setOrder($col = '', $dir='') {
        $this->order_by_col = $col;
        $this->order_direction = $dir;
    }

}

?>
